package handler

import (
	"boy-go/middlewares"
	"boy-go/modules/system/service"
	"boy-go/modules/system/vo"
	"boy-go/pkg/response"
	"boy-go/pkg/security"
	"boy-go/pkg/xnum"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
)

// AddSysMenuRouter 添加路由
func AddSysMenuRouter(group *gin.RouterGroup) {
	group.GET("/menu/:id", middlewares.HasPermission("system:menu:query"), get_menu)
	group.GET("/menu/list", middlewares.HasPermission("system:menu:list"), list_menu)
	group.POST("/menu", middlewares.HasPermission("system:menu:add"), middlewares.Logger("菜单管理", middlewares.INSERT), add_menu)
	group.PUT("/menu", middlewares.HasPermission("system:menu:edit"), middlewares.Logger("菜单管理", middlewares.UPDATE), edit_menu)
	group.DELETE("/menu/:ids", middlewares.HasPermission("system:menu:remove"), middlewares.Logger("菜单管理", middlewares.DELETE), del_menu)
	group.GET("/menu/treeselect", treeselect_menu)
	group.GET("/menu/roleMenuTreeselect/:roleId", roleMenuTreeselect_menu)
}

// get_menu
// @Summary 获取菜单权限表详情
// @Description 获取菜单权限表详情（管理页面）
// @Tags system-menu
// @Accept json
// @Produce json
// @Param id path string true "ID"
// @Success 200 {object} response.R[vo.SysMenuModel] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/menu/{id} [get]
func get_menu(ctx *gin.Context) {
	var idStr = ctx.Param("id")
	id := xnum.Str2int64(idStr)
	m, err := service.NewSysMenuService().GetById(id)
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxSuccess(ctx, m)
}

// list_menu
// @Summary 获取菜单权限表列表
// @Description 获取菜单权限表列表（管理页面）
// @Tags system-menu
// @Accept json
// @Produce json
// @Success 200 {object} response.R[PageRes[vo.SysMenuModel]] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/menu/list [get]
func list_menu(ctx *gin.Context) {
	req := vo.QuerySysMenuReq{}
	if err := ctx.BindQuery(&req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	list, err := service.NewSysMenuService().SelectMenuListByUserId(&req, *security.GetLoginUserId())
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	resDatas := make([]*vo.SysMenuModel, 0)
	for _, menu := range list {
		resData := vo.SysMenuModel{}
		if copier.Copy(&resData, menu) != nil {
			continue
		}
		resDatas = append(resDatas, &resData)
	}
	response.AjaxSuccess(ctx, resDatas)
}

func treeselect_menu(ctx *gin.Context) {
	req := vo.QuerySysMenuReq{}
	if err := ctx.BindQuery(&req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	list, err := service.NewSysMenuService().SelectMenuListByUserId(&req, *security.GetLoginUserId())
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxSuccess(ctx, service.NewSysMenuService().BuildMenuTreeSelect(list))
}

// add_menu
// @Summary 新增菜单权限表
// @Description 新增菜单权限表（管理页面）
// @Tags system-menu
// @Accept json
// @Produce json
// @Param req body  vo.AddSysMenuReq true "req"
// @Success 200 {object} response.R[vo.AddSysMenuRsp] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/menu [post]
func add_menu(ctx *gin.Context) {
	var req vo.AddSysMenuReq
	if err := ctx.ShouldBind(&req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	m, err := service.NewSysMenuService().Add(&req)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, m)
	}
}

// edit_menu
// @Summary 编辑菜单权限表
// @Description 编辑菜单权限表（管理页面）
// @Tags system-menu
// @Accept json
// @Produce json
// @Param req body  vo.EditSysMenuReq true "req"
// @Success 200 {object} response.R[vo.EditSysMenuRsp] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/menu [put]
func edit_menu(ctx *gin.Context) {
	var req vo.EditSysMenuReq
	if err := ctx.ShouldBind(&req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	m, err := service.NewSysMenuService().Edit(&req)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, m)
	}
}

// del_menu
// @Summary 删除菜单权限表
// @Description 删除菜单权限表（管理页面）
// @Tags system-menu
// @Accept json
// @Produce json
// @Success 200 {object} response.R[string] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/menu [delete]
func del_menu(ctx *gin.Context) {

}

func roleMenuTreeselect_menu(ctx *gin.Context) {
	var idStr = ctx.Param("roleId")
	roleId := xnum.Str2int64(idStr)
	menus, err := service.NewSysMenuService().SelectMenuListByUserId(nil, *security.GetLoginUserId())
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	}
	ajax := response.NewAjaxResultSuccess()
	ajax.Put("checkedKeys", service.NewSysMenuService().SelectMenuListByRoleId(roleId))
	ajax.Put("menus", service.NewSysMenuService().BuildMenuTreeSelect(menus))
	response.AjaxResponse(ctx, ajax)
}
